package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCode;
import com.javasm.entity.ReturnResult;
import com.javasm.service.Impl.adminMenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/queryUser")
public class QueryUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* 允许跨域访问相关参数设置 */
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
        /* 允许跨域的请求⽅法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        req.setCharacterEncoding("utf-8");
        String pagestr = req.getParameter("page");
        String pagesizestr = req.getParameter("pagesize");

        Integer page =1;
        Integer pagesize = 10;
        if(pagestr != null && !"".equals(pagestr)){
            page = Integer.parseInt(pagestr);

        }if(pagesizestr != null && !"".equals(pagesizestr)){
            pagesize = Integer.parseInt(pagesizestr);

        }



        String menuname = req.getParameter("menuname");
        String pidstr = req.getParameter("pid");
        //
        Long pid = null;
        if(pidstr != null && !"".equals(pidstr)){
            pid = Long.parseLong(pidstr);
        }
            AdminMenu inputMenu = new AdminMenu(menuname, pid);


        //2调用service
        adminMenuServiceImpl adminMenuService = new adminMenuServiceImpl();
        //页面总记录
        Integer total = adminMenuService.countMenuByDao(inputMenu);



        List<AdminMenu> adminMenus = adminMenuService.ListMenuByCondition(inputMenu,page, pagesize);

        //根据执行结果，返回数据
        ReturnResult returnResult = new ReturnResult();

        if(adminMenus.size() > 0){
            returnResult.setReturnCode(ReturnCode.QUERY_SUCCESS.getCode());
            returnResult.setReturnMsg(ReturnCode.QUERY_SUCCESS.getMsg());
            PageInfo pageInfo = new PageInfo(page,pagesize,total);
            returnResult.setReturnData(adminMenus);
            returnResult.setPageInfo(pageInfo);

        }else {
            returnResult.setReturnCode(ReturnCode.QUERY_NO_DATA.getCode());
            returnResult.setReturnMsg(ReturnCode.QUERY_NO_DATA.getMsg());


        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();


    }}
